import { HttpEvent,HttpInterceptor,HttpHandler,HttpRequest,HttpResponse} from "@angular/common/http";
import { Injectable } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
import { catchError, mergeMap } from 'rxjs/operators';

import { of } from "rxjs/observable/of"

@Injectable()
export class InterceptorProvider implements HttpInterceptor {
intercept(req:HttpRequest<any>,next:HttpHandler):Observable<HttpEvent<any>>{
    const authReq = req.clone({
      
    });
 
    return <any>next.handle(authReq).pipe(mergeMap((event: any) => {
        if (event instanceof HttpResponse && event.status != 200) {
          return ErrorObservable.create(event);
        }
        return Observable.create(observer => observer.next(event)); //请求成功返回响应
      }),
      catchError((res: HttpResponse<any>) => {
        switch (res.status) {
          case 401:
            break;
          case 200:
            console.log('业务错误');
            break;
          case 404:
          console.log('404');
            break;
          case 403:
            console.log('业务错误');
            break;
        }
         return of(event);
      }));
  }
}
